package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.LatentClient;
import cn.wolfcode.crm.query.LatentClientQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.Dispose;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("resourceViewClient")
public class ResourceViewController {
    @Autowired
    private ILatentClientService latentClientService;

    @RequestMapping("resourceView")
    //@RequiresPermissions(value = {"resourceViewClient:resourceView", "资源池客户界面"}, logical = Logical.OR)
    @RequiresPermissions("resourceViewClient:resourceView")
    public String resourceView() {
        return "resourceViewClient/list";
    }

    @RequestMapping("selectClientByStatus")
    @ResponseBody
    //@RequiresPermissions(value = {"resourceViewClient:selectClientByStatus", "资源池客户查询"}, logical = Logical.OR)
    @RequiresPermissions("resourceViewClient:selectClientByStatus")
    public PageResult selectClientByStatus(LatentClientQueryObject qo) {
        return latentClientService.selectClientByStatus(qo);
    }


    /**
     * CLIENT_LATENT  资源池客户移入潜在客户
     * @param id
     * @return
     */
    @RequestMapping("changeState2Latent")
    @ResponseBody
    public Dispose changeState2Latent(Long id) {

        Dispose result = new Dispose();
        try {
            latentClientService.changeState2Latent(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("吸纳失败");
        }

    }
    @RequestMapping("changeState")
    @ResponseBody
    public Dispose changeState(Long id) {

        Dispose result = new Dispose();
        try {
            latentClientService.changeState(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("更改状态失败");
        }

    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    //@RequiresPermissions(value = {"latentClient:view", "资源池的更新和修改"}, logical = Logical.OR)
    @RequiresPermissions("latentClient:view")
    public Dispose saveOrUpdate(LatentClient entity) {
        Dispose result = new Dispose();
        try {
            if (entity.getId() == null) {
                latentClientService.insert(entity);
            }else {
                latentClientService.updateByPrimaryKey(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("保存失败");
        }
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<LatentClient> listAll() {
        return latentClientService.listAll();
    }


    /**  根据客户id查询客户所有信息  */
    @RequestMapping("selectApplicantById")
    @ResponseBody
    public LatentClient selectApplicantById(Long id) {
        return latentClientService.selectApplicantById(id);
    }
}
